﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;


namespace DesignLife
{
    public partial class Contact : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCookie();
            }
        }

        private void BindCookie()
        {
            var httpCookie = Response.Cookies["ReturnURL"];
            if (httpCookie != null)
                httpCookie.Value = "Default.aspx";
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtCapcha.Text.Equals(Session["captcha"].ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    Contacts ct = new Contacts();
                    ct.subject = txtsubject.Text;
                    ct.fullname = txtfullname.Text;
                    ct.Email = txtEmail.Text;
                    ct.Message = txtMessage.Text;
                    ct.DateTime = DateTime.Now;
                    
                    string host = "";
                    string hostName = "";
                    string ipNum = "";
                    IPHostEntry ip = new IPHostEntry();
                    host = Dns.GetHostName();
                    ip = Dns.GetHostEntry(host);
                    hostName = ip.HostName;
                    /*---------------------------End Get IP--------------------------*/

                    foreach (IPAddress listip in ip.AddressList)
                    {
                        ipNum = listip.ToString();
                    }
                    ct.IPClient = ipNum;
                    //ct.Status = 1;
                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["Design_LifeConnection"].ConnectionString;
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandText = "INSERT INTO Contacts ([Subject],FullName,Email,[Message],[DateTime]) VALUES (@Subject,@FullName, @Email, @Message, @DateTime)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@FullName", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Message", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Subject", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@DateTime", SqlDbType.DateTime);

                    cmd.Parameters[0].Value = ct.fullname;
                    cmd.Parameters[1].Value = ct.Email;
                    cmd.Parameters[2].Value = ct.Message;
                    cmd.Parameters[3].Value = ct.subject;
                    cmd.Parameters[4].Value = ct.DateTime;
                    int result = cmd.ExecuteNonQuery();

                    if (result == 1)
                    {
                        lbTest.Visible = true;
                        lbTest.Text = "Successfull";
                    }
                    else
                    {
                        lbTest.Visible = true;
                        lbTest.Text = "failed";
                    }
                    lbthongbao.Text = "nhập mã xác nhận thành công !";
                }
                else
                {
                    lbthongbao.Text = "vui lòng nhập mã xác nhận !";
                }

                conn.Close();

            }
            catch
            {
                lbTest.Visible = true;
                lbTest.Text = "failed";
            }
        }

        protected void Reset_Click(object sender, EventArgs e)
        {
            txtfullname.Text = "";
            txtEmail.Text = "";
            txtsubject.Text = "";
            lbTest.Visible = false;
        }

        protected void rbtCaptcha_Click(object sender, ImageClickEventArgs e)
        {
            txtCapcha.Focus();
        }
    }
}